package com.xyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyh.mapper.StudentMapper;
import com.xyh.pojo.entity.Student;
import com.xyh.pojo.vo.StudentVo;
import com.xyh.service.StudentService;
import com.xyh.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
* @author 丁德成
* @description 针对表【student】的数据库操作Service实现
* @createDate 2023-04-12 18:52:22
*/
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper stuMapper;

    @Override
    public Result findByPage(Integer current, Integer pageSize, StudentVo stuVo) {
        Page<Student> page = new Page<>(current,pageSize);//分页对象

        QueryWrapper<Student> wrapper = new QueryWrapper<>();//条件
        //判断名称
        if(StringUtils.hasText(stuVo.getSname())){
            wrapper.like("sname",stuVo.getSname());
        }
        //依据时间来查询
        if(Objects.nonNull(stuVo.getEarlyDate())){
            wrapper.ge("sdate",stuVo.getEarlyDate());
        }
        if(Objects.nonNull(stuVo.getLastDate())){
            wrapper.ge("sdate",stuVo.getLastDate());
        }
        IPage<Student> page1 = stuMapper.findPage(page, wrapper);

        return new Result(200,"分页条件查询成功",page1);
    }

    @Override
    public Result findById(Integer smpno) {
        Student student = stuMapper.selectById(smpno);
        return new Result(200,"查询结果",student);
    }

    @Override
    public Result insertOneSOS(Student student) {
        return new Result(200,"插入完成",stuMapper.insert(student));
    }

    @Override
    public Result deleteByIdSOS(Integer smpno) {
        return new Result(200,"删除成功",stuMapper.deleteById(smpno));
    }

    @Override
    public Result updateSOS(Student student) {
        return new Result(200,"修改成功",stuMapper.updateById(student));
    }
}
